<?php
include_once 'app/until/GetData.php';

class EditFile extends GetData{

	private $content;
	private $where;
	private $file;
	public $User_Id;

	public function __construct($match){
		$this->User_Id = $match['User_Id'];
		$this->where = $match['Wo'];
		$this->file = $match['Datei'];

		if(!empty($_SESSION ['UserId']) and is_numeric($_SESSION ['UserId'])){
			$this->User_Id = $_SESSION ['UserId'];
		}else{
			HTTP::redirect ( BASE_URL . '/NotLogin' );
		}
		 
		if($this->User_Id != $_SESSION['UserId']){
			HTTP::redirect ( BASE_URL . '/NotYou?q=It isnt your File.' );
		}
		 
	}

	public function getWort(RainTPL $design){
		$DBMail = new Db_Mail();
		$this->design = $design;
		if(isset($this->design->post->newcontent)){
			$data = $this->design->post->newcontent;
			$this->saveContent('media/user/'.$this->User_Id.'/'.$this->where.'/'.$this->file,$data);
		}
		$this->content = $this->getContent('media/user/'.$this->User_Id.'/'.$this->where.'/'.$this->file);
		$this->design->assign ('UnReadedMails',$DBMail->GetCountUnReadedMails($_SESSION ['UserId']));
		$this->design->assign ('Content',$this->content);
		
	    if( $cache = $this->design->cache( $this->design->ln.'/User/edit/file', $expire_time = 600 ) )
		{
		 	echo $cache;
		}
		else
		{
		 	$this->design->draw($this->design->ln.'/User/edit/file');
		}
	}

	private function getContent($pfad){
		$dateihandler 	= 	fopen ($pfad,"r+");
		$datei 			=	fread ($dateihandler,filesize($pfad) );
		fclose($dateihandler);
			
		return $datei;
	}
	private function saveContent($pfad,$data){
		if (is_writable($pfad) and GFW_Validate::ValidateFile($data)){
			$dateihandler 	= 	fopen ($pfad,"w");

			if(fwrite($dateihandler,$data) != false){
				fclose($dateihandler);
				return true;
			}else{
				fclose($dateihandler);
				return false;
			}
		}else{
			return false;
		}
	}

}
?>